import { type OrderItem } from "./types";

// 订单状态描述
const statusTextMap = {
  0: "待支付",
  1: "待核销",
  2: "已完成",
  3: "已取消",
};

// 支付方式描述
const paymentTypeMap = {
  0: "现金支付",
  1: "积分支付",
};

// 生成订单号
const generateOrderNo = () => {
  return (
    "ORD" + Date.now().toString().slice(-8) + Math.floor(Math.random() * 1000)
  );
};

// 生成随机时间
const generateRandomDate = (daysBack = 30) => {
  const date = new Date();
  date.setDate(date.getDate() - Math.floor(Math.random() * daysBack));
  return date.toISOString();
};

// 项目列表
const projects = [
  {
    name: "面部护理套餐",
    image: "https://img.zcool.cn/community/01d9f35acee0d1a80120927b1d8e1c.jpg",
  },
  {
    name: "全身SPA按摩",
    image: "https://img.zcool.cn/community/01901f5acee0d2a8012092342e3ea9.jpg",
  },
  {
    name: "头部按摩护理",
    image: "https://img.zcool.cn/community/01e7545acee0d3a801219c7721dde7.jpg",
  },
  {
    name: "美甲服务套餐",
    image: "https://img.zcool.cn/community/01add15acee0d5a8012192a1bdafb1.jpg",
  },
  {
    name: "精油SPA套餐",
    image: "https://img.zcool.cn/community/0149565acee0d6a801219c7786d56f.jpg",
  },
];

// 产品列表
const products = [
  {
    name: "面膜套装",
    image: "https://img.zcool.cn/community/01947d5acee0d7a8012092347d0ea7.jpg",
  },
  {
    name: "护肤精油",
    image: "https://img.zcool.cn/community/01caf65acee0d8a801209234d9e0a8.jpg",
  },
  {
    name: "护发精华液",
    image: "https://img.zcool.cn/community/01b6365acee0d9a801219c77c952b8.jpg",
  },
  {
    name: "眼部护理套装",
    image: "https://img.zcool.cn/community/01ed815acee0daa801209234d2afe8.jpg",
  },
];

// 生成模拟订单数据
export const generateMockOrders = (count = 10): OrderItem[] => {
  const mockOrders: OrderItem[] = [];

  for (let i = 0; i < count; i++) {
    // 不再生成状态0（待支付）的订单，只生成状态1-3的订单
    const status = Math.floor(Math.random() * 3) + 1; // 1-3
    const useProject = Math.random() > 0.5;
    const createTime = generateRandomDate();
    const paymentTime = generateRandomDate(status === 0 ? 0 : 30);
    const totalSessions = Math.floor(Math.random() * 10) + 1;
    const usedSessions =
      status === 2
        ? totalSessions
        : status === 1
          ? Math.floor(Math.random() * totalSessions)
          : 0;
    const remainingSessions = totalSessions - usedSessions;
    const paymentType = Math.floor(Math.random() * 2);

    const selectedProject =
      projects[Math.floor(Math.random() * projects.length)];
    const selectedProduct =
      products[Math.floor(Math.random() * products.length)];

    mockOrders.push({
      id: 100000 + i,
      orderNo: generateOrderNo(),
      projectName: useProject ? selectedProject.name : "",
      productName: !useProject ? selectedProduct.name : "",
      projectCoverImgUrl: useProject ? selectedProject.image : "",
      productCoverImgUrl: !useProject ? selectedProduct.image : "",
      totalPrice: Math.floor(Math.random() * 100000) / 100 + 100,
      totalSessions,
      remainingSessions,
      usedSessions,
      status,
      statusDesc: statusTextMap[status as keyof typeof statusTextMap],
      paymentType: status > 0 ? paymentType : undefined,
      paymentTypeDesc:
        status > 0
          ? paymentTypeMap[paymentType as keyof typeof paymentTypeMap]
          : "",
      paymentTime: status > 0 ? paymentTime : "",
      createTime,
    });
  }

  return mockOrders;
};

// 生成分页数据
export const getMockOrderList = (params: any) => {
  const { pageNumber = 1, pageSize = 10, status } = params;

  // 生成100条数据作为总数据库
  const allOrders = generateMockOrders(100);

  // 根据status筛选
  const filteredOrders =
    status !== null && status !== undefined
      ? allOrders.filter((order) => order.status === status)
      : allOrders;

  // 分页
  const startIndex = (pageNumber - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  const records = filteredOrders.slice(startIndex, endIndex);

  return {
    code: 200,
    data: {
      records,
      total: filteredOrders.length,
      pages: Math.ceil(filteredOrders.length / pageSize),
      current: pageNumber,
      size: pageSize,
    },
    message: "操作成功",
  };
};
